package com.nowcoder.community.controller;

import com.nowcoder.community.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

/**
 * @ClassName DataController
 * @Description
 * @Author hhwu
 * @Date 2022/6/1 15:09
 * Version 1.0
 **/
@Controller
public class DataController {

    @Autowired
    private DataService dataService;

    @RequestMapping(path = "/data", method = {RequestMethod.GET, RequestMethod.POST})// 这里的post请求是因为该方法会结受下面的post请求方法转发的请求
    public String getDataPage() {
        /**
         * @Author hhwu
         * @Description //TODO 访问统计界面
         * @Date 15:11 2022/6/1
         * @Param []
         * @return java.lang.String
         **/
        return "/site/admin/data";
    }

    @RequestMapping(path = "/data/uv", method = RequestMethod.POST)
    public String getUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,// 指定和页面上表单传来的日期格式一致
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model) {
        /**
         * @Author hhwu
         * @Description //TODO 统计指定日期区间网站UV
         * @Date 15:12 2022/6/1
         * @Param [start, end, model]
         * @return java.lang.String
         **/
        long uv = dataService.calculateUV(start, end);
        model.addAttribute("uvResult", uv);
        model.addAttribute("uvStartDate", start);
        model.addAttribute("uvEndDate", end);
        return "forward:/data";// 转发到/data这个controller，在页面上显示结果数据
    }

    @RequestMapping(path = "/data/dau", method = RequestMethod.POST)
    public String getDAU(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model) {
        /**
         * @Author hhwu
         * @Description //TODO 统计活跃用户
         * @Date 15:20 2022/6/1
         * @Param [start, end, model]
         * @return java.lang.String
         **/
        long dau = dataService.calculateDAU(start, end);
        model.addAttribute("dauResult", dau);
        model.addAttribute("dauStartDate", start);
        model.addAttribute("dauEndDate", end);
        return "forward:/data";
    }

}
